package	birdry.utils	{
	
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.system.Capabilities;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	import flash.display.Loader;
	import flash.events.ProgressEvent;
	import flash.events.Event;
	import flash.net.LocalConnection;
	import flash.external.ExternalInterface;
	import flash.net.sendToURL;
	import flash.events.IOErrorEvent;
	import flash.utils.Dictionary;
	
	
	public class NetUtil	{
		
		public static var dic_loadMovie_loader:Dictionary = new Dictionary(true);
		public static var dic_loadMovie_bytesLoaded:Dictionary = new Dictionary(true);
		public static var dic_loadMovie_bytesTotal:Dictionary = new Dictionary(true);
		public static var dic_loadMovie_bytesPer:Dictionary = new Dictionary(true);
		
		public static function externalInterfaceCall( functionName:String, ...rest ): void	{
			rest.unshift( functionName );
			if(ExternalInterface.available)		ExternalInterface.call.apply('null', rest);
			rest.shift();
			trace('externalInterfaceCall -> 함수 = ' + functionName + ', 인자 = ' + rest);
		}
		
		/**
		* navigateToURL
		* @param	url		:	url경로
		* @param	window	:	"_self", "_blank"
		* @return
		*/
		public static function navigateToURL (url: String, window: String = "_self"): void	{
			if ( Capabilities.playerType == "External")	{
				//로컬
				trace( prototype.constructor.toString() + " : " + url + " - window : " + window);
			}	else	{
				//브라우저
				flash.net.navigateToURL( new URLRequest( url ) , window );
			}
			/*
			switch( Capabilities.playerType )			{
				case "StandAlone":
				break;
				case "External":
					//로컬
					trace( prototype.constructor.toString() + " : " + url );
				break;
				case "PlugIn":
				break;
				case "ActiveX":
					//브라우저
					flash.net.navigateToURL( new URLRequest( url ) , window );
				break;
			}
			*/
		}
		
		public static function sendToURL( url: String ): void	{
			flash.net.sendToURL( new URLRequest(url) );
		}
		
		/**
		* loadMovie
		* @param	loader	:	로드할 로더객체
		* @param	url			:	로드할 파일 url경로
		* @return
		*/
		public static function loadMovie(loader:Loader, url:String, callBackComplete:Function = null, callBackProgress:Function = null, callBackIOError:Function = null):void	{
			
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
			loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgress);
			loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
			
			loader.load( new URLRequest(url) );
			
			function onComplete( e: Event ): void	{
				if (callBackComplete != null)	callBackComplete(loader);
				removeEvent();
			}
			function onProgress( e: ProgressEvent ): void	{
				dic_loadMovie_bytesLoaded[loader] = e.bytesLoaded;
				dic_loadMovie_bytesTotal[loader] = e.bytesTotal;
				dic_loadMovie_bytesPer[loader] = e.bytesLoaded / e.bytesTotal;
				if (callBackProgress != null)	callBackProgress(loader, loader);
			}
			function ioErrorHandler( e: IOErrorEvent ): void	{
				trace(IOErrorEvent(e).text);
				if (callBackIOError != null)	callBackIOError(loader, e);
				removeEvent();
			}
			function removeEvent(): void	{
				loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onComplete);
				loader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, onProgress);
				loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
			}
		}
		
		/**
		 * 실제 swf파일이 있는 서버의 도메인
		 * 로컬 = localhost
		 * 서버 = http:// 를 제외한 도메인이름
		 */
		public static function swfDomain(): String	{
			var lc:LocalConnection = new LocalConnection;
			return String( lc.domain.toString() );
		}
		
		public static function htmlDomain(): String	{
			return String( ExternalInterface.call("function() { return document.location.href.to.String(); }") );
		}
	}	// class
}	// package